import * as Type from '@/apis/controltotal/type';
import clonedeep from 'lodash.clonedeep';
interface CtlDetailType extends Type.SubRelDetail {
  subCode: string;
}

/**
 *
 * @param data 获取的控件数据
 * @param dataType 返回的数据结构 'tableHead'：表头 | 'ctlDetail'：控件详情 | 'subDetail'：子集及子集下面的控件详情
 */
export function changeControlData(
  data: Type.FuncCTLInfos,
  dataType: 'tableHead' | 'ctlDetail' | 'subDetail'
) {
  switch (dataType) {
    case 'tableHead':
      return changeTableHead(data);

    case 'ctlDetail':
      return changeCtlDetail(data);

    case 'subDetail':
      return changeSubDetail(data);

    default:
      return changeSubDetail(data);
  }
}

/**
 * 处理表头数据
 * @param data
 * @returns 表头信息
 */
function changeTableHead(data: Type.FuncCTLInfos) {
  const { children } = data;
  const newTableHead: Type.CTLDetail[] = [];
  children?.forEach((ele) => {
    delete ele?.ctlDetail?.id;
    ele?.ctlDetail ? newTableHead.push(ele?.ctlDetail) : null;
  });
  return newTableHead;
}

/**
 * 处理控件数据
 * @param data
 * @returns 控件信息
 */
function changeCtlDetail(data: Type.FuncCTLInfos) {
  const { children } = data;
  console.log(
    '🚀 ~ file: control.ts:53 ~ changeCtlDetail ~ children:',
    children
  );
  const newCtlDetail: CtlDetailType[] = [];
  children?.forEach((ele) => {
    ele?.subDetail?.subRelDetail.forEach((item) => {
      const newData = clonedeep(item);
      newData.subCode = ele?.subDetail?.subCode;
      newCtlDetail.push(newData);
    });
  });
  return newCtlDetail;
}

/**
 * 处理子集数据
 * @param data
 * @returns 子集信息
 */
function changeSubDetail(data: Type.FuncCTLInfos) {
  const { children } = data;
  const newSubDetail: Type.SubDetail[] = [];
  children?.forEach((ele) => {
    delete ele?.subDetail?.id;
    ele?.subDetail ? newSubDetail.push(ele.subDetail) : null;
  });
  return newSubDetail;
}

/**
 * 处理部门数据
 * @param data
 * @returns 部门数组
 */
export const changeDepartmentData = (data: any) => {
  const ids: number[] = [];
  const newData: any = [];
  data.forEach((ele: any) => {
    ele.controlData.forEach((item: any) => {
      item.code === 'C3D33FA57216F4968BDE51C31D623FC09' && item.value === '2'
        ? ids.push(ele.id)
        : null;
    });
  });
  data.forEach((ele: any) => {
    if (ids.indexOf(ele.id) !== -1) {
      ele.controlData.forEach((item: any) => {
        item.code === 'C30BBF36A5AF541F7A2657671DEE2EDD6'
          ? newData.push({ id: item.id, name: item.value })
          : null;
      });
    }
  });
  return newData;
};

/**
 * 查找控件值
 * arr 人员详情接口返回的useTemplate.collectionModel
 * ctlCode 要找的控件code
 */
export const getControlValue = (arr: any, ctlCode: string) => {
  arr = changeCtlDetail2Flat(arr);
  const res = arr.find((el: any) => el?.ctlCode === ctlCode);
  return res?.ctlValue;
};

/**
 * 处理控件数据扁平化
 * @param data
 * @returns 控件信息
 */
function changeCtlDetail2Flat(data: any) {
  const newCtlDetail: CtlDetailType[] = [];
  data?.forEach((el: any) => {
    el?.controlModel.forEach((item: any) => {
      const newData = clonedeep(item);
      newData.ctlValue = newData?.ctlValue?.ctlValue;
      newData.subCode = el?.subCode;
      newCtlDetail.push(newData);
    });
  });
  return newCtlDetail;
}
